package com.edfu.core.coursemgt.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.edfu.core.contentmgt.jcr.service.api.IContentMgtService;
import com.edfu.core.contentmgt.jcr.util.Constants;
import com.edfu.core.coursemgt.persistence.entity.CourseOfferingEntity;
import com.edfu.core.coursemgt.persistence.entity.LessonEntity;
import com.edfu.core.coursemgt.persistence.entity.LessonUnitEntity;

@Service
public class ContentMgtHook {
	
	@Autowired
	private IContentMgtService contentMgtService;
	public IContentMgtService getContentMgtService() {
		return contentMgtService;
	}


	public String addCourseOfferingNode(CourseOfferingEntity coe) {
		Map<String, String > propertiesMap = new HashMap<String, String>();
		propertiesMap.put(Constants.EDU_NODENAME, coe.getCode());
		propertiesMap.put(Constants.EDU_NODEDESC, coe.getDescription());
		propertiesMap.put(Constants.EDU_NODECATEGORY,  CourseMgtConstants.CourseContentContextType.COURSEOFFERING.toString());
		String fqp = "/CourseOffering/"+coe.getCode();
		return getContentMgtService().addDirectory(fqp, propertiesMap).getFullyQualifiedContentName();
	}
	
	public String addLessonNode(LessonEntity le) {
		Map<String,String> propertiesMap = new HashMap<String, String>();
		propertiesMap.put(Constants.EDU_NODENAME, le.getName());
		propertiesMap.put(Constants.EDU_NODEDESC, le.getDescription());
		propertiesMap.put(Constants.EDU_NODECATEGORY, CourseMgtConstants.CourseContentContextType.LESSON.toString());
		String fqp = "/CourseOffering/"+le.getCourseOffering().getCode()+"/Lesson/"+le.getName();
		return getContentMgtService().addDirectory(fqp, propertiesMap).getFullyQualifiedContentName();
	}
	
	public String addLessonUnitNode(LessonUnitEntity lue) {
		Map<String,String> propertiesMap = new HashMap<String, String>();
		propertiesMap.put(Constants.EDU_NODENAME, lue.getName());
		propertiesMap.put(Constants.EDU_NODEDESC, lue.getTitle());
		propertiesMap.put(Constants.EDU_NODECATEGORY, CourseMgtConstants.CourseContentContextType.LESSON_UNIT.toString());
		String fqp = "/CourseOffering/"+lue.getLesson().getCourseOffering().getCode()+"/Lesson/"+lue.getLesson().getName() + "/LessonUnit/" + lue.getName();
		return getContentMgtService().addDirectory(fqp, propertiesMap).getFullyQualifiedContentName();
	}
	
	public String addContent(InputStream contentAsStream,
			String ctxPath, String contentType,String contentId,
			Map<String, String> propertiesMap) {
		String fqp = ctxPath + "/" + contentType + "/" + contentId;
		return getContentMgtService().addFile(fqp, propertiesMap, contentAsStream).getFullyQualifiedContentName();
	}

	
}
